package com.jcli._自己的想法._文件流._获取路径;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class SimpleFileManager extends JFrame {
    private JTextField directoryTextField;

    public SimpleFileManager() {
        setTitle("文件管理器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setLocationRelativeTo(null);

        initUI();

        setVisible(true);
    }

    private void initUI() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        directoryTextField = new JTextField();
        directoryTextField.setEditable(false);
        panel.add(directoryTextField, BorderLayout.CENTER);

        JButton openButton = new JButton("打开目录");
        openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openDirectory();
            }
        });
        panel.add(openButton, BorderLayout.SOUTH);

        add(panel);
    }

    private void openDirectory() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedDirectory = fileChooser.getSelectedFile();
            directoryTextField.setText(selectedDirectory.getAbsolutePath());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SimpleFileManager();
            }
        });
    }
}
